package main

import "C"

import (
	"context"
	"fmt"
	"log"

	"google.golang.org/grpc"
	grpchello "google.golang.org/grpc/examples/helloworld/helloworld"
)

type GrpcClient struct {
	addr   string
	conn   *grpc.ClientConn
	client grpchello.GreeterClient
}

func NewGrpcClient() *GrpcClient {
	return &GrpcClient{}
}

func (c *GrpcClient) Dial(add string) {
	var err error
	c.addr = c.addr
	c.conn, err = grpc.Dial("127.0.0.1:50051", grpc.WithInsecure())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
		return
	}
	c.client = grpchello.NewGreeterClient(c.conn)
}

func (c *GrpcClient) SayHello(msg string) {
	r, err := c.client.SayHello(context.Background(), &grpchello.HelloRequest{Name: msg})
	if err != nil {
		log.Println("err", err)
		return
	}
	fmt.Println(r)
}

func (c *GrpcClient) Close() {
	c.conn.Close()
}

//export SendHello
func SendHello(msg string) {
	client := NewGrpcClient()
	client.Dial("127.0.0.1:50051")
	client.SayHello("youle")
}

func main() {}
